<?php

namespace app\admin\controller;

use think\facade\Db;
use app\admin\model\Link as LinkM;
use think\facade\Request;
use think\facade\View;

class Link extends Common
{

    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        if (Request::isPost()) {
            $page = input("request.page", 1, "intval");
            $limit = input("request.limit", 10, "intval");
            $ary_get = input("request.");
            $ary_get['pageall'] = 10;
            $list = Db::name("link")->page($page, $limit)->order('id', 'desc')->select()->toArray();
            return ['code' => 0, 'msg' => '获取成功！', 'data' => $list];
        }
        return view();
    }


    //添加分组
    public function add()
    {
        try {
            if (request()->isPost()) {
                $data = input('post.');
                $data['create_time'] = time();
                LinkM::create($data);
                cache('linkList', null);
                return ['code' => 0, 'msg' => '友链添加成功！'];
            } else {
                return View::fetch();
            }
        } catch (\Exception $e) {
            return ['code' => 99999, 'msg' => $e->getMessage()];
        }
    }

    //修改友情链接
    public function edit()
    {
        try {
            if (request()->isPost()) {
                $data = input('post.');
                $where['id'] = $data['id'];
                LinkM::update($data, $where);
                cache('linkList', null);
                return ['code' => 0, 'msg' => '友链修改成功！'];
            } else {
                $id = input('id');
                $info = LinkM::find(['id' => $id]);
                $this->assign('info', $info);
                return View::fetch();
            }
        } catch (\Exception $e) {
            return ['code' => 99999, 'msg' => $e->getMessage()];
        }
    }

}